<template>
	<div class="app-container">
		<el-form ref="queryForm" :model="form" size="mini" :inline="true" label-width="70px">
			<el-row>
				<el-col :span="6">
					<el-form-item label="药店名称">
						<el-input v-model="drugStoreName" :disabled="true"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="患者姓名">
						<el-input v-model="form.params.patientName" placeholder="请输入患者姓名..."></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="手机号" prop="phone">
						<el-input v-model="form.params.phone" placeholder="请输入手机号..."></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="订单号" prop="code">
						<el-input v-model="form.code" placeholder="请输入订单号..."></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="6">
					<el-form-item label="处方号" prop="prescribeId">
						<el-input v-model="form.prescribeId" placeholder="请输入处方号..."></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="支付状态" prop="payStatus">
						<el-select v-model="form.payStatus" placeholder="请选择支付状态..." style="width: 90%;">
							<el-option v-for="item in payStatus" :key="item.dictCode" :label="item.dictLabel" :value="item.dictValue"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="发药状态" prop="deliveryStatus">
						<el-select v-model="form.deliveryStatus" placeholder="请选择支付状态..." style="width: 92%;">
							<el-option v-for="item in deliveryStatus" :key="item.dictCode" :label="item.dictLabel" :value="item.dictValue"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="订单状态" prop="orderStatus">
						<el-select v-model="form.status" placeholder="请选择订单状态..." style="width: 92%;">
							<el-option v-for="item in orderStatus" :key="item.dictCode" :label="item.dictLabel" :value="item.dictValue"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="下单时间">
				<el-date-picker v-model="value" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期"
					end-placeholder="结束日期" value-format="yyyy-MM-dd" :picker-options="pickerOptions" @change="handleChange">
				</el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" @click="getDrugOrderList">查询</el-button>
				<el-button icon="el-icon-refresh" @click="reset">重置</el-button>
			</el-form-item>
		</el-form>
		<el-divider></el-divider>
		<el-table :data="tableData" style="width: 100%" size="mini" v-loading="tableLoading" stripe class="patientOrder">
			<el-table-column type="expand">
				<template slot-scope="scope">
					<div class="drugsBox">
						<ul class="drugsUl">
							<li class="drugItem" v-for="(item,index) in scope.row.prescribe.preDrugs" :key="index">
								<div class="checkboxDiv"><el-checkbox v-model="checked"></el-checkbox></div>
								<div class="drugsName">
									<p class="sizeWeight">{{item.drug.name}}</p>
									<p class="drugsDes">{{formatDrugType(item.drug.drugType)}}  {{item.drug.smallestUnitNumber}}{{formatSmallestUnit(item.drug.smallestUnit)}}/{{formatRetailUnit(item.drug.retailUnit)}} {{item.drug.manufacturer}}</p>
								</div>
								<div class="drugsPrice sizeWeight">{{item.drug.retailPrice}} (元)</div>
								<div class="drugsUnit sizeWeight">{{item.drug.dosageUnitNumber}} （{{formatDosageUnit(item.drug.dosageUnit)}}）</div>
								<div class="drugsAllPrice sizeWeight">{{item.drug.retailPrice * item.drugUnit}} (元)</div>
							</li>
						</ul>
						<div class="allAmount">
							<div class="grid-content bg-purple totalAmounts">
								总金额：
								<span class="amounts">{{ scope.row.prescribe.lastTotal  }}</span>元
							</div>
							<div v-if="scope.row.deliveryStatus ==0" class="totalStatus-1">代发货</div>
							<div v-if="scope.row.deliveryStatus ==1" class="totalStatus-2">已发货</div>
							<div v-if="scope.row.deliveryStatus ==2" class="totalStatus-3">已签收</div>
							<div class="storePay">药店支付</div>
						</div>
						<div class="seeDetail">
							<div class="grid-content bg-purple">
								<el-button type="success" size="mini" plain @click="delivery((scope.row))" :disabled="scope.row.deliveryStatus===1 || scope.row.status === 1">确认发药</el-button>
								<div class="seeMore">
									<el-link type="primary">查看详情</el-link>
								</div>
							</div>
						</div>
					</div>
				</template>
			</el-table-column>
			<el-table-column type="index" label="序号" :index="indexMethod"></el-table-column>
			<el-table-column prop="createTime" label="下单时间"></el-table-column>
			<el-table-column prop="code" label="订单号"></el-table-column>
			<el-table-column prop="prescribeId" label="处方号"></el-table-column>
			<el-table-column label="患者">
				<template slot-scope="scope">{{scope.row.patient.patientName}}</template>
			</el-table-column>
			<el-table-column label="手机号">
				<template slot-scope="scope">{{scope.row.patient.phone}}</template>
			</el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope">
					<el-button type="text" size="mini" @click="delivery(scope.row)" :disabled="scope.row.deliveryStatus===1 || scope.row.status===1">确认发药</el-button>
					<el-button type="text" size="mini" :disabled="scope.row.deliveryStatus===1 || scope.row.status===1">配送</el-button>
				</template>
			</el-table-column>
		</el-table>
		<pagination
			v-show="total>0"
			:total="total"
			:page.sync="form.params.pageNum"
			:limit.sync="form.params.pageSize"
			@pagination="getDrugOrderList"
		/>
	</div>
</template>

<script>
import { GetDrugOrderList, EditDrugOrder } from '@/api/order/drug'
import {SubscribeDelivery} from "@/api/server"

export default {
	data() {
		return {
			multipleSelection:[],
			totalStatus:0,
			checked: true,
			form: {
				code: undefined,
				prescribeId: undefined,
				payStatus: undefined,
				deliveryStatus: undefined,
				status:undefined,
				params: {
					patientName: undefined,
					phone: undefined,
					beginTime: undefined,
					endTime: undefined,
					pageNum: 1,
					pageSize: 10,
				}
			},
			drugStoreName: '大润发药店',
			value: '',
			pickerOptions: {
				shortcuts: [{
					text: '最近一周',
					onClick(picker) {
						const end = new Date();
						const start = new Date();
						start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
						picker.$emit('pick', [start, end]);
					}
				}, {
					text: '最近一个月',
					onClick(picker) {
						const end = new Date();
						const start = new Date();
						start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
						picker.$emit('pick', [start, end]);
					}
				}, {
					text: '最近三个月',
					onClick(picker) {
						const end = new Date();
						const start = new Date();
						start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
						picker.$emit('pick', [start, end]);
					}
				}],
			},
			payStatus: [],
			deliveryStatus: [],
			orderStatus: [],
			tableData: [],
			tableLoading: false,
			total: 0,
			drugType:[],
			smallestUnit:[],
			retailUnit:[],
			dosageUnit: []
		};
	},
	computed: {
		formatDrugType() {
			return (val) => {
				if(val !== null) {
					for (let i = 0; i < this.drugType.length; i++) {
						if(this.drugType[i].dictValue == val) {
							return this.drugType[i].dictLabel;
						}
					}
				}
				return val;
			}
		},
		formatSmallestUnit() {
			return (val) => {
				if(val !== null) {
					for (let i = 0; i < this.smallestUnit.length; i++) {
						if(this.smallestUnit[i].dictValue == val) {
							return this.smallestUnit[i].dictLabel;
						}
					}
				}
				return val;
			}
		},
		formatRetailUnit() {
			return (val) => {
				if(val !== null) {
					for (let i = 0; i < this.retailUnit.length; i++) {
						if(this.retailUnit[i].dictValue == val) {
							return this.retailUnit[i].dictLabel;
						}
					}
				}
				return val;
			}
		},
		formatDosageUnit() {
			return (val) => {
				if(val !== null) {
					for (let i = 0; i < this.dosageUnit.length; i++) {
						if(this.dosageUnit[i].dictValue == val) {
							return this.dosageUnit[i].dictLabel;
						}
					}
				}
			}
		}
	},
	mounted() {
		this.getSysDict();
		this.getDrugOrderList();
	},
	methods: {
		handleSelectionChange(val) {
			this.multipleSelection = val;
		},
		getDrugOrderList() {
			this.tableLoading = true;
			GetDrugOrderList(this.form).then(res => {
				let lastTotal
				let row = res.rows;
				row.forEach((item,index) => {
					let arr = item.prescribe.preDrugs.map(ele => ele.drug.retailPrice * ele.drugUnit)
					lastTotal =  eval(arr.join("+"));
					item.prescribe.lastTotal = lastTotal
				});
				this.tableData = row;
				this.total = res.total;
				this.tableLoading = false;
			});
		},
    delivery(row) {
      this.$confirm('您确认要发货吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        EditDrugOrder({orderId: row.orderId, deliveryStatus: 1}).then(res => {
          if (res.code === 200) {
            this.msgSuccess('您的操作已提交成功');
			SubscribeDelivery({orderId: row.orderId, userId: row.patient.userId});
            this.getDrugOrderList();
          }
        });
      });
    },
		handleChange() {
			this.form.params.beginTime = this.value[0];
			this.form.params.endTime = this.value[1];
		},
    reset() {
		  this.value = '';
      this.form.params.beginTime = undefined;
      this.form.params.endTime = undefined;
		this.form.status = undefined;
      this.resetForm("queryForm");
    },
		getSysDict() {
			this.getDicts("pay_status").then(response => {
				this.payStatus = response.data;
			});
			this.getDicts("delivery_status").then(response => {
				this.deliveryStatus = response.data;
			});
			this.getDicts("status").then(response => {
				this.orderStatus = response.data;
			});
			// 药品分类
			this.getDicts("drug_classify").then(response => {
				this.drugType = response.data;
			});
			// 零售单位
			this.getDicts("retail_unit").then(response => {
				this.retailUnit = response.data;
			});
			// 最小单位
			this.getDicts("smallest_unit").then(response => {
				this.smallestUnit = response.data;
			});
			// 剂量单位
			this.getDicts("dosage_unit").then(response => {
				this.dosageUnit = response.data;
			})
		},
		indexMethod(index) {
			return (this.form.params.pageNum - 1) * this.form.params.pageSize + index + 1;
		}
	}
}
</script>

<style lang="scss" scoped>
.drugsBox{
	width: 1100px;
	display: flex;
	flex-wrap:wrap;
	height: auto;
	margin: 10px;
}
.allAmount{
	width: 150px;
	padding-top: 15px;
	padding-left: 20px;
    position: relative;
	border-top: 1px solid #f2f2f2;
	border-bottom: 1px solid #f2f2f2;
}
.seeDetail{
	width: 150px;
	text-align: center;
	padding-top: 30px;
	border: 1px solid #f2f2f2;
}
.totalAmounts{
	font-size: 26upx;
	font-weight: 700;
	color: #333333;
}
.amounts{
	font-size: 26upx;
	font-weight: 700;
	color: red;
	margin: 0 5px;
}
.totalStatus-1,.totalStatus-2,.totalStatus-3{
	font-size: 26upx;
	font-weight: 700;
	margin-top: 10px;
}
.totalStatus-1{
	color: #33CCFF;
}
.totalStatus-2{
	color: #00CC00;
}
.totalStatus-3{
	color: #FF9900;
}
.storePay{
	font-size: 26upx;
	font-weight: 700;
	color: #FF66FF;
	position: absolute;
    bottom: 10px;
}
.seeMore{
	margin: 10px 0 0 0px;
}
.drugsUl{
	width: 640px;
	padding: 0;
	margin: 0;
	border: 1px solid #f2f2f2;
}
.drugItem{
	width: 100%;
	height: 70px;
	display: flex;
	flex-wrap:wrap;
	align-items: center;
	border-bottom: 1px solid #f2f2f2;
    padding-left: 35px;
}
.drugsUl li:last-child{
	border-bottom:none;
}
.checkboxDiv{
	width: 40px;
}
.drugsName{
	width: 260px;
}
.drugsPrice,.drugsUnit{
	width: 100px;
	text-align: center;
}
.sizeWeight{
	font-weight: 700;
    font-style: normal;
    font-size: 28upx;
	color: #333333;
}
.drugsDes{
	font-size: 12px;
    color: #999999;
}
.drugsAllPrice{
	width: 100px;
    text-align: center;
    height: 70px;
    line-height: 70px;
    border-left: 1px solid #f2f2f2;
}

</style>
<style>
.patientOrder .el-table__expanded-cell{
	padding: 0;
}

.addBorder{
	text-align: center;
	border: 1px solid #dee6eb;
	border-top: none;
}
.borderLeft{
	border-left:  1px solid #dee6eb;
}
.borderTop{
	border-top:  1px solid #dee6eb;
}
</style>
